package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.DeviceAlarmDao;
import com.sugon.entity.DeviceAlarmEntity;
import com.sugon.service.DeviceAlarmService;
import com.sugon.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * ${comments}Service实现类
 *
 * @author sugon
 * @date 2022-11-18 11:51:11
 */
@Service("deviceAlarmService")
public class DeviceAlarmServiceImpl implements DeviceAlarmService {
    @Autowired
    private DeviceAlarmDao deviceAlarmDao;

    @Override
    public void ensureExists(String deviceType, String code, String msg) {
        List<DeviceAlarmEntity> alarmList = deviceAlarmDao.selectList(deviceType, code);
        if (CollectionUtil.isNotEmpty(alarmList)) {
            return;
        }
        DeviceAlarmEntity alarm = new DeviceAlarmEntity();
        alarm.setDeviceType(deviceType);
        alarm.setCode(code);
        alarm.setMsg(msg);
        alarm.setRemark("系统自动添加");
        save(alarm);
    }

    @Override
    public DeviceAlarmEntity queryObject(String id) {
        return deviceAlarmDao.queryObject(id);
    }

    @Override
    public List<DeviceAlarmEntity> queryList(Map<String, Object> map) {
        return deviceAlarmDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return deviceAlarmDao.queryTotal(map);
    }

    @Override
    public int save(DeviceAlarmEntity deviceAlarm) {
        deviceAlarm.setId(RandomUtil.randomString(32));
        deviceAlarm.setDelFlag(Constant.IS_NOT_DEL_INT);
        deviceAlarm.setCreateTime(DateUtil.date());
        return deviceAlarmDao.save(deviceAlarm);
    }

    @Override
    public int update(DeviceAlarmEntity deviceAlarm) {
        deviceAlarm.setUpdateTime(DateUtil.date());
        return deviceAlarmDao.update(deviceAlarm);
    }

    @Override
    public int delete(String id) {
        return deviceAlarmDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return deviceAlarmDao.deleteBatch(ids);
    }
}
